/**
 * 文件服务
 * @author Peter
 */
angular.module('imsApp').factory('FileManager', ['$http','api', 'paramsManager', 'FileUploader', function ($http,api, paramsManager, FileUploader) {
    var FileManager = {

        /**
         * 获得文件
         * alias：后台接收字段，必须
         * url：上传文件接口，必须
         * maxSize：单个文件尺寸限制，默认1M
         * maxItem：上传文件数量限制，默认1个
         * type：上传文件类型，多个类型时放数组里，默认不限制格式
         */
        getFile: function (fileBody,back) {
            var MAXSIZE = fileBody.maxSize?fileBody.maxSize:1;
            var MAXITEM = fileBody.maxItem?fileBody.maxItem:1;
            var uploader= new FileUploader({
                //autoUpload:fileBody.autoUpload?fileBody.autoUpload:false,//是否自动上传
                alias:fileBody.alias,
                queueLimit: MAXITEM,//文件个数
                // removeAfterUpload: true,//上传后删除文件 */
                filters: [{
                    name: fileBody.alias,//后台接收字段
                    fn: function (item /*{File|FileLikeObject}*/, options) {
                        var temp = [];
                        //判断文件类型
                        if(fileBody.type){
                            for(var i=0;i<fileBody.type.length;i++){
                                var s = (fileBody.type[i]).toLowerCase();
                                temp.push(s);
                            }
                            if (temp.indexOf((item.name).substring(item.name.lastIndexOf('.') + 1).toLowerCase())==-1) {
                               // back.checkImage = "E";
                                return false;
                            }
                        }
                        //判断文件尺寸
                        if (item.size > MAXSIZE* 1024 * 1024) {
                           // back.checkImage = "E";
                            return false;
                        } else {
                            //去重
                            if (this.queue.length > 0) {
                                var isExsit = false;
                                for (var i = 0; i < this.queue.length; i++) {
                                    if (this.queue[i].file.name == item.name) {
                                        isExsit = true;
                                        break;
                                    }
                                }
                                if (isExsit) {
                                    return false;
                                } else {
                                    fileBody.uploadSuccess = false;
                                    return true;
                                }
                            } else {
                                fileBody.uploadSuccess = false;
                                return true;
                            }
                        }

                    }
                }],
                url: fileBody.url,
                headers: {'X-Auth-Token': "123"}

            });
            return uploader;
        },
        /**
         * 上传文件
         * @param scope
         *      $scope.scansPathList 存返回路径
         *      $scope.uploadAllSuccess 判断是否全部上传成功
         *      之所以要传$scope，是因为文件上传有一个过程，直接绑定到某个字段上不能成功
         * @param image 要上传的文件对象
         * @param isNew 是否是新建
         */
        uploadFile: function (urlList, image, isNew) {
            if (image.queue.length > 0) {
                //上传
                image.uploadAll();
                image.onErrorItem = function (fileItem, response, status, headers) {
                    alert("网络出现故障，上传失败,请刷新重新添加");   //未达到后台上传失败
                };
                //成功上传
                var successNo = 0;
                image.onSuccessItem = function (fileItem, response, status, headers) {
                    if (response.c == "s") {
                        successNo++;
                        if (isNew) {//新建
                            urlList = urlList=="undefined" ||urlList==null  ?
                                response.filePath : urlList + "," + response.filePath;
                        } else {//修改
                            if (successNo == 1) {//修改时上传了第一张，应当将之前的路径拼接串覆盖
                                urlList= response.filePath;
                            } else {
                                urlList = urlList + "," + response.filePath;
                            }
                        }
                       /* if (successNo == image.queue.length) {
                            return urlList;
                           // scope.uploadAllSuccess = true;
                        }*/
                    } else if (response.c == 'f') {
                        fileItem.isSuccess = false;
                        alert("上传  " + fileItem.file.name + " 失败，请删除后重新添加上传");
                    } else {
                        fileItem.isSuccess = false;
                        alert(response.i);
                    }
                };
                image.onCompleteAll = function(){
                    return urlList;
                }
            }
        }
    }
    return FileManager;
}]);
